﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

using System.Data.Entity;
using DAO;

namespace BUS
{
    public class BUS_WRENTRY
    {
        WRMOEntities db = new WRMOEntities();

        public int _Count()
        {
            return db.WRENTRies.Count();
        }

        public IList<WRENTRY> _GetList()
        {
            return db.WRENTRies.ToList();
        }

        public IList<WRENTRY> _GetList(Func<WRENTRY, bool> cond)
        {
            return db.WRENTRies.Where(cond).ToList();
        }

        public WRENTRY _GetObject(int ID)
        {
            return db.WRENTRies.Where(p => p.ID == ID).FirstOrDefault();
        }

        public WRENTRY _GetObject(Func<WRENTRY, bool> cond)
        {
            return db.WRENTRies.Where(cond).FirstOrDefault();
        }

        public WRENTRY _CreateObject()
        {
            return db.WRENTRies.CreateObject();
        }

        public void _Save()
        {
            db.SaveChanges();
        }

        public void _Add(WRENTRY entity, ref int id, bool save = true)
        {
            try
            {
                db.AddToWRENTRies(entity);
                if (save)
                    _Save();
                id = entity.ID;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        public void _Add(WRENTRY entity, bool save = true)
        {
            try
            {
                db.AddToWRENTRies(entity);
                if (save)
                    _Save();
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        

        public void _Update(WRENTRY entity)
        {
            try
            {
                WRENTRY _mainEntity = db.WRENTRies.Where(p => p.ID == entity.ID).FirstOrDefault();
                if (_mainEntity != null)
                {
                    _mainEntity.ID = entity.ID;
                    _mainEntity.EntryName = entity.EntryName;
                    _mainEntity.EntContent = entity.EntContent;
                    _mainEntity.UpdateDate = entity.UpdateDate;
                    _mainEntity.IDUpdateBy = entity.IDUpdateBy;
                    _mainEntity.Viewed = entity.Viewed;
                    _mainEntity.Price = entity.Price;
                    _mainEntity.PriceDesc = entity.PriceDesc;
                    _mainEntity.IDPriceType = entity.IDPriceType;
                    _mainEntity.NameToContact = entity.NameToContact;
                    _mainEntity.PhoneToContact = entity.PhoneToContact;
                    _mainEntity.AddressToContact = entity.AddressToContact;
                    _mainEntity.EmailToContact = entity.EmailToContact;
                    _mainEntity.IDUrgent = entity.IDUrgent;
                    _mainEntity.UrgenFromDate = entity.UrgenFromDate;
                    _mainEntity.UrgentToDate = entity.UrgentToDate;
                    _mainEntity.UrgentStatus = entity.UrgentStatus;
                    _mainEntity.IDCategoryLv = entity.IDCategoryLv;
                    _mainEntity.IDEntType = entity.IDEntType;
                    _mainEntity.IDSaleType = entity.IDSaleType;
                    _mainEntity.IDUserApprove = entity.IDUserApprove;
                    _mainEntity.IDCity = entity.IDCity;
                    _mainEntity.Active = entity.Active;
                    _mainEntity.Keywords = entity.Keywords;
                }
                _Save();
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public void _Delete(int ID)
        {
            try
            {
                WRENTRY _mainEntity = db.WRENTRies.Where(p => p.ID == ID).FirstOrDefault();
                if (_mainEntity != null)
                {
                    db.WRENTRies.DeleteObject(_mainEntity);
                }
                _Save();
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public void _Delete(WRENTRY entity)
        {
            try
            {
                WRENTRY _mainEntity = db.WRENTRies.Where(p => p.ID == entity.ID).FirstOrDefault();
                if (_mainEntity != null)
                {
                    db.WRENTRies.DeleteObject(_mainEntity);
                }
                _Save();
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        public DataTable Get_Entry_List()
        {
            IList<Get_Entry_List_Result> query = db.Get_Entry_List().ToList();
            return Common.Common.ConvertToDataTable(query.ToList());
        }
        public DataTable Get_Entry_View(int idcity, int idcatelv, int from, int to)
        {
            IList<Get_Entry_View_Result> query = db.Get_Entry_View(idcatelv, idcity,from, to).ToList();
            return Common.Common.ConvertToDataTable(query.ToList());
        }
        public DataTable Get_Entry_View_UT(int idcity, int idcatelv, int from, int to)
        {
            IList<Get_Entry_View_UT_Result> query = db.Get_Entry_View_UT(idcatelv, idcity, from, to).ToList();
            return Common.Common.ConvertToDataTable(query.ToList());
        }
        public DataTable Get_Entry_Search(int idcity, int saletype, int from, int to)
        {
            IList<Get_Entry_Search_Result> query = db.Get_Entry_Search(idcity, from, to,saletype).ToList();
            return Common.Common.ConvertToDataTable(query.ToList());
        }
    }
}
